#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

#include "process.h"

int main()
{
  int i = 0;
  pthread_t tid[5] = {0};
  unsigned int *return_code = NULL;
  unsigned int recycle_code = 0;
  printf("This is father thread, thread id = %u, start to create son thread.\n",
         pthread_self());
  for(i = 0; i < 5; i++) {
    pthread_create(&tid[i], NULL, thread_print, NULL);
  }

  for(i = 0; i < 5; i++) {
    recycle_code = pthread_join(tid[i],(void **)(&return_code));
    if (return_code && (recycle_code == 0)) {
      printf("Success to recycle thread %u, return code is %u\n",
             tid[i], *return_code);
      free(return_code);
    } else if (recycle_code == 0) {
      printf("Success to recycle thread %u, but fail to get recycle_code\n",
             tid[i]);
    } else {
      printf("Fail to recycle thread %u\n", tid[i]);
    }
  }
  printf("This is father thread, thread id = %u, start to exit.\n",
         pthread_self());
  
  pthread_exit(NULL);
}
